package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.entity.TradeInfo;
import com.example.entity.vo.TradeDetailVO;
import com.example.mapper.TradeDetailMapper;
import com.example.mapper.TradeInfoMapper;
import com.example.service.TradeInfoService;
import com.example.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 交易信息服务实现类
 */
@Service
public class TradeInfoServiceImpl extends ServiceImpl<TradeInfoMapper, TradeInfo> implements TradeInfoService {

    @Autowired
    private TradeDetailMapper tradeDetailMapper;

    /**
     * 根据卡号查询交易记录
     */
    @Override
    public Result<List<TradeDetailVO>> getTradesByCardId(String cardId) {
        LambdaQueryWrapper<TradeDetailVO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TradeDetailVO::getCardId, cardId);
        queryWrapper.orderByDesc(TradeDetailVO::getTradeDate);

        List<TradeDetailVO> tradeDetails = tradeDetailMapper.selectList(queryWrapper);
        return Result.success(tradeDetails);
    }

    /**
     * 获取最近的交易记录
     */
    @Override
    public List<TradeDetailVO> getRecentTrades(int limit) {
        LambdaQueryWrapper<TradeDetailVO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(TradeDetailVO::getTradeDate);
        queryWrapper.last("LIMIT " + limit); // 限制返回记录数

        return tradeDetailMapper.selectList(queryWrapper);
    }
}